const namespace = 'z';

// z-button-box__element--modifier
const _bem = (
  prefixName: string,
  blockSuffix: string,
  element: string,
  modifier: string
) => {
  if (blockSuffix) prefixName += `-${blockSuffix}`;
  if (element) prefixName += `__${element}`;
  if (modifier) prefixName += `--${modifier}`;
  return prefixName;
};

const createBem = (prefixName: string) => {
  const b = (blockSuffix: string = '') =>
    _bem(prefixName, blockSuffix, '', '');

  const e = (element?: string) =>
    element ? _bem(prefixName, '', element, '') : '';

  const m = (modifier?: string) =>
    modifier ? _bem(prefixName, '', '', modifier) : '';

  const is = (name: string, state: unknown) =>
    state ? `is-${name}` : '';

  const bm = (block: string = '', modifier: string = '') =>
    _bem(prefixName, block, '', modifier);

  return {
    b,
    e,
    m,
    is,
    bm,
  };
};

export const createNamespace = (name: string) => {
  return createBem(namespace + '-' + name);
};
